$(function () {
    $("#task_grid").datagrid({
        /*自适应*/
        fit: true,
        /*向此地址发送请求获得数据填充表格*/
        url: '/taskExecute/query.do',
        columns: [/*此处为第一层列标题*/[
            {field: "data", title: "日期", width: 100},
            {
                field: "employee", title: "员工姓名", width: 100, formatter: function (value) {
                    return value ? value.username : ''
                }
            },
            {
                field: "employee_dept", title: "部门", width: 100, formatter: function (value, row) {
                    return row.employee.dept ? row.employee.dept.name : ""
                }
            },
            {
                field: "task", title: "任务编号", width: 100, formatter: function (value, row) {
                    return value ? value.taskno : ''
                }
            },

            {
                field: "state", title: "状态", width: 100, formatter: function (value, row) {

                    return value ? "<font color='#ff69b4'>已完成</font>" : "<font color='red'>未完成</font>"
                }
            },
            {
                field:'a',title:"查看任务", width: 100, formatter: function (value, row) {
                    return "<a href='javascript:check()'><p style='color: #56C9F5'>查看</p></a>"
                }
            }

        ]/*此处可以插入第二层标题*/],
        //为表格顶端的按键
        toolbar: "#tb",
        //真正的自动展开收缩列的大小,以适应网格的宽度,防水平滚动
        fitColumns: true,
        //可在行底显示分页工具栏
        pagination: true,
        //行码
        rownumbers: true,
        //只允许单选某行
        singleSelect: true,
        onClickRow:function (index,a) {
            if(a.state){
                $("#statebtn").linkbutton({
                    disabled:true
                })
                $("#editbtn").linkbutton({
                    disabled:true
                })
            }else{
                $("#statebtn").linkbutton({
                    disabled:false
                })
                $("#editbtn").linkbutton({
                    disabled:false
                })
            }
        }
    })
    //对弹窗进行初始化
    $("#task_dialog").dialog({
        width: 350,
        height: 450,
        resizable: true,
        buttons: '#form_btn',
        closed: true,
        onClose:function () {
            $("#task_form").form("clear")
        }

    })
})
function opendialog() {
    $("#userid").show();
    $("#taskid").show();
    $("#task_dialog").dialog('setTitle', '新建任务');
    $("#task_dialog").dialog('open');
}

function reload() {
    $("#task_grid").datagrid('reload');
    $("#statebtn").linkbutton({
        disabled:false
    })
    $("#editbtn").linkbutton({
        disabled:false
    })
}

//表单中的按键
function query() {
    var keyword = $("[name=keyword]").val();
    var deptId = $("[name=deptId]").val();
    var startTime = $("[name=startTime]").val();
    var finalTime = $("[name=finalTime]").val();
    $("#task_grid").datagrid('load', {
        keyword: keyword,
        deptId: deptId,
        startTime: startTime,
        finalTime: finalTime

    })
}
function save() {

    $("#task_form").form('submit', {
        url: '/taskExecute/saveOrUpdate.do',
        success: function (data) {
            data = $.parseJSON(data);
            if (data.success) {
                $.messager.alert('温馨提示', '保存成功！', 'info', function () {
                    reload();
                    cancel();
                });
            } else {
                $.messager.alert('温馨提示', data.msg, 'warning');
            }
        }
    })
}

function edit() {
    var row = $("#task_grid").datagrid("getSelected");
    if (!row) {
        $.messager.alert("温馨提示", "请选中一条数据", "warning");
        return;
    }
    if(row.task){
        row["taskno"] = row.task.taskno;
        row["date"] = row.task.date;
        row["site"] = row.task.site;
        row["taskword"] = row.task.taskword;
        row["tid"]=row.tid;
    }
    if(row.employee){
        row["employeeId"] =row.employee.id;
    }

    $("#userid").hide();
    $("#taskid").hide();
    row["employeeId"]=row.employeeId
    $("#task_form").form('load',row);
    $("#task_dialog").dialog('setTitle', '编辑任务');
    $("#task_dialog").dialog('open');

}
function cancel() {
    $("#task_dialog").dialog('close');
}
function stateojbk() {
    var row = $("#task_grid").datagrid("getSelected");
    if (!row) {
        $.messager.alert("温馨提示", "请选中一条数据", "warning");
        return;
    }
    $.post("/taskExecute/editstate.do",{tid:row.tid},function (data) {
        if (data.success) {
            $.messager.alert('温馨提示', '该任务完成',"info", function () {
                cancel();
                reload();
                alert("我真的佛了")
            });
        } else {
            $.messager.alert('温馨提示', data.msg);
        }
    })
}
function check() {
    var row = $("#task_grid").datagrid("getSelected");
    if (!row) {
        $.messager.alert("温馨提示", "请选中一条数据", "warning");
        return;
    }
    if(row.task){
        row["taskno"] = row.task.taskno;
        row["date"] = row.task.date;
        row["site"] = row.task.site;
        row["taskword"] = row.task.taskword;
        row["tid"]=row.tid;
    }
    if(row.employee){
        row["employeeId"] =row.employee.id;
    }

    $("#userid").hide();
    $("#taskid").hide();
    row["employeeId"]=row.employeeId
    $("#task_form").form('load',row);
    $("#task_dialog").dialog('setTitle', '查看');
    $("#task_dialog").dialog('open');


}
function exportXls() {
    window.location.href="/taskExecute/exportXls.do";
}

function tdelete() {

    var row = $("#task_grid").datagrid("getSelected");
    if (!row) {
        $.messager.alert("温馨提示", "请选中一条数据", "warning");
        return;
    }
    $.post("/taskExecute/delete.do",{tid:row.tid},function (data) {
        console.log(data);
        if (data.success) {
            $.messager.alert('温馨提示', '删除成功！',"info", function () {
                reload();
            });
        } else {
            $.messager.alert('温馨提示', data.msg);
        }
    })
}